import axios from "axios";
import project from "@/views/project/project.vue";
// 定义一个常量来保存统一的url
const baseUrl = "http://localhost:8080";
/**
 * home组件中请求数据的对象
 * @type {{}}
 */
const projectRequest = {
    /**
     * 加载所有的菜单,我们这里准备异步请求，需要一个回调函数
     * @param project 对象，用来封装查询条件
     * @param page 页码，默认是1
     * @param limit 每页大小，默认10
     * @param callBack 回调函数
     */
    loadProjects(project, page = 1, limit = 10, callBack) {
        let url = baseUrl + "/project/list?page=" + page + "&limit=" + limit;
        if (project) {
            if (project.projectName) {
                url += "&projectName=" + project.projectName;
            }
            if (project.startTimeBegin) {
                url += "&startTimeBegin=" + project.startTimeBegin;
            }
            if (project.startTimeEnd) {
                url += "&startTimeEnd=" + project.startTimeEnd;
            }
            if (project.endTimeBegin) {
                url += "&endTimeBegin=" + project.endTimeBegin;
            }
            if (project.endTimeEnd) {
                url += "&endTimeEnd=" + project.endTimeEnd;
            }
            if (project.projectStatus) {
                url += "&projectStatus=" + project.projectStatus;
            }
            if (project.userId){
                url += "&userId=" + project.userId
            }
        }
        // 发送请求
        axios.get(url)
            .then(res => {
                // 回调函数是从外面传入的，我们要将我们请求得到的结果传回到回调函数中.
                // 由于axiso会对结果进行一个封装，所以我们这里传入回调函数的数据直接就是res.data
                // 我们在外面获取的数据就是我们后端响应的数据本身。
                callBack(res.data);
            })
    },//新增项目
    saveNewProject(newProject, callBack) {
        let url = baseUrl + "/project/insert";
        axios.post(url, newProject)
            .then(res => {
                // 回调函数是从外面传入的，我们要将我们请求得到的结果传回到回调函数中.
                // 由于axiso会对结果进行一个封装，所以我们这里传入回调函数的数据直接就是res.data
                //  // 我们在外面获取的数据就是我们后端响应的数据本身。
                callBack(res.data);
            })
    },//通过id查询项目信息,来展示项目对应的详情

    selectProjectDetailsByProjectId(projectId, callBack) {

        // console.log("project.js中的id为:" + projectId)
        let url = baseUrl + "/project/selectByProjectId?projectId=" + projectId
        axios.get(url)
            .then(res => {
                callBack(res.data)
            })
    },
    updateProjectById(project, callBack) {
        if (project) {
            let url = baseUrl + "/project/update";
            axios.put(url, project)
                .then(res => {
                    callBack(res.data)
                })
        }
    }, getProjectById(projectId, callBack) {
        axios.get(baseUrl + "/project/get?projectId=" + projectId)
            .then(res => callBack(res.data));
    }, deleteProject(projectId, callBack) {
        let url = baseUrl + "/project/delete?projectId=" + projectId;
        axios.delete(url)
            .then(res => callBack(res.data))
    },
    archiveProject(projectId, callBack) {
        let url = baseUrl + "/project/archive?projectId=" + projectId;
        axios.put(url)
            .then(res => callBack(res.data))
    }
    , releases(projectIds, callBack) {
        let url = baseUrl + "/project/releases?a=1";
        if (projectIds) {
            projectIds.forEach(id => url += '&projectId=' + id)
        }
        axios.put(url)
            .then(res => callBack(res.data))
    }, archives(projectIds, callBack) {
        let url = baseUrl + "/project/archives?a=1";
        if (projectIds) {
            projectIds.forEach(id => url += "&projectId=" + id)
        }
        axios.put(url)
            .then(res => callBack(res.data))
    }, deletesProject(projectIds, callBack) {
        let url = baseUrl + "/project/deletes?a=1";
        if (projectIds) {
            projectIds.forEach(id => url += "&projectId=" + id)
        }
        axios.delete(url)
            .then(res => callBack(res.data))
    },
    /**
     * 加载所有的菜单,我们这里准备异步请求，需要一个回调函数
     * @param project 对象，用来封装查询条件
     * @param page 页码，默认是1
     * @param limit 每页大小，默认10
     * @param callBack 回调函数
     */
    loadDeleteProjects(project,page=1,limit=10,callBack) {
        let url = baseUrl + "/project/listDeletes?page=" + page + "&limit=" + limit;
        if (project) {
            if (project.projectName) {
                url += "&projectName=" + project.projectName;
            }
            if (project.startTimeBegin) {
                url += "&startTimeBegin=" + project.startTimeBegin;
            }
            if (project.startTimeEnd) {
                url += "&startTimeEnd=" + project.startTimeEnd;
            }
            if (project.endTimeBegin) {
                url += "&endTimeBegin=" + project.endTimeBegin;
            }
            if (project.endTimeEnd) {
                url += "&endTimeEnd=" + project.endTimeEnd;
            }
            if (project.projectStatus) {
                url += "&projectStatus=" + project.projectStatus;
            }
            if (project.userId) {
                url += "&userId=" + project.userId;
            }
        }
        // 发送请求
        axios.get(url)
            .then(res=>{
                // 回调函数是从外面传入的，我们要将我们请求得到的结果传回到回调函数中.
                // 由于axiso会对结果进行一个封装，所以我们这里传入回调函数的数据直接就是res.data
                // 我们在外面获取的数据就是我们后端响应的数据本身。
                callBack(res.data);
            })
    },
    reReleases(projectIds,callBack){
        let url = baseUrl + "/project/reReleases?a=1";
        if(projectIds){
            projectIds.forEach(id=>url += "&projectId="+id)
        }
        axios.put(url).then(res=>callBack(res.data));
    }

}
// 将projectRequest暴露出去
export default projectRequest;